package utilities;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.TreeMap;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;



import org.w3c.dom.*;

//import com.sun.org.apache.xml.internal.serialize.XMLSerializer;


public class DragonConfig {

  Map commanders = new TreeMap();	

  Params myParams;
  String fileName;
  String applyRefresh;
  int scanDelay=1000;
  int refreshDelay=3; 
  boolean shouldexit = false;

  Document doc;
  
  public  DragonConfig(String fileName) {
	  this.fileName = fileName;
	  refresh();	  
  }
  

 public Map getCommanders()
 {
	 return commanders;
 }
 public boolean getExit()
 {
	 return shouldexit;
 }
  
 public String getLogFile()
 {
	 return this.myParams.getOutFileName();
 }

 
 public  void refresh() {

	 
  try {
  File file = new File(fileName);
  
  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  DocumentBuilder db = dbf.newDocumentBuilder();
  doc = db.parse(file);
  doc.getDocumentElement().normalize();
  
  commanders = new TreeMap();	
  
  /*
   * Parameters section
   */
  NodeList nodeLst = doc.getElementsByTagName("params");  

  String outFileName=null;
  String scanDelay=null; 
  String appDir=null;
  String refreshDelay=null;
  applyRefresh=null;

  nodeLst = doc.getElementsByTagName("outFileName");
  for (int s = 0; s < nodeLst.getLength(); s++) {
	  	Node tn = nodeLst.item(s);
	  	outFileName = tn.getChildNodes().item(0).getNodeValue();
	  	//tn.getChildNodes().item(0).setNodeValue("TestOutValue");
	  	break;
  }
  
  nodeLst = doc.getElementsByTagName("appDir");
  for (int s = 0; s < nodeLst.getLength(); s++) {
	  	Node tn = nodeLst.item(s);
	  	appDir = tn.getChildNodes().item(0).getNodeValue();
	  	//tn.getChildNodes().item(0).setNodeValue("TestOutValue");
	  	break;
  }
  
  nodeLst = doc.getElementsByTagName("scanDelay");
  for (int s = 0; s < nodeLst.getLength(); s++) {
	  scanDelay = nodeLst.item(s).getChildNodes().item(0).getNodeValue();
	    break;
  }
 
  nodeLst = doc.getElementsByTagName("refreshDelay");
  for (int s = 0; s < nodeLst.getLength(); s++) {
	   refreshDelay = nodeLst.item(s).getChildNodes().item(0).getNodeValue();
	    break;
  } 
  
  nodeLst = doc.getElementsByTagName("exit");
  String eStr=null;
  for (int s = 0; s < nodeLst.getLength(); s++) {
	  eStr = nodeLst.item(s).getChildNodes().item(0).getNodeValue();
	    break;
  } 
  if(eStr.equals("true")) shouldexit = true;
  
  myParams = new Params(outFileName, appDir, scanDelay, refreshDelay);
  
  
  
  /*
   * commanders configuration
   * 
   */
  nodeLst = doc.getElementsByTagName("commander");  
  
  String name=null;
  String note=null;;
  String configFile=null;
  String className=null;
  
  Node oNode = null;
  Node mn=null;
  
  
  for (int s = 0; s < nodeLst.getLength(); s++) {
	  Node fstNode = nodeLst.item(s);
	  Element fstElmnt = (Element) fstNode;
	  String eType="";
      NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("note");
	  for (int n = 0; n < fstNmElmntLst.getLength(); n++) {
		    note = fstNmElmntLst.item(n).getChildNodes().item(0).getNodeValue();
		    break;
	  }
       fstNmElmntLst = fstElmnt.getElementsByTagName("name");
	  for (int n = 0; n < fstNmElmntLst.getLength(); n++) {
		  name = fstNmElmntLst.item(n).getChildNodes().item(0).getNodeValue();
		    break;
	  }	   
      fstNmElmntLst = fstElmnt.getElementsByTagName("configFile");
	  for (int n = 0; n < fstNmElmntLst.getLength(); n++) {
		  configFile = fstNmElmntLst.item(n).getChildNodes().item(0).getNodeValue();
		    break;
	  }	   
      fstNmElmntLst = fstElmnt.getElementsByTagName("type");
	  for (int n = 0; n < fstNmElmntLst.getLength(); n++) {
		  eType = fstNmElmntLst.item(n).getChildNodes().item(0).getNodeValue();
		    break;
	  }	   
	  Object ec = "";
	  if(eType.equals("engine"))
	  {
		  ec = new EngineCommander(name, note, myParams.appDir, configFile);	  
	  }
	  else
	  {
		  ec = new ProcessorCommander(name, note, myParams.appDir, configFile);	  		  
	  }
	  commanders.put(name, ec);   
  }
  
  
  } catch (Exception e) {
    e.printStackTrace();
  }
 }
 public int getScanDelay()
 {
	 return myParams.getScanDelay();
 }
 public int getRefreshDelay()
 {
	 return myParams.getRefreshDelay();
 }
 public boolean applyRefresh()
 {
	 if(applyRefresh.equals("true"))
		 return true;
	 else
		 return false;
 }
 
}  



 